﻿
Option Strict On

Imports System.Net.Sockets
Imports osi.root.connector
Imports osi.root.utils
Imports osi.service.device
Imports osi.service.commander

Partial Public Class powerpoint
    Public Function as_herald(ByVal c As TcpClient,
                              ByRef o As herald) As Boolean
        Dim b As block = Nothing
        If as_block(c, b) Then
            o = New block_herald_adapter(b)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function as_block(ByVal c As TcpClient,
                             ByRef o As block) As Boolean
        Dim f As flow = Nothing
        If as_flow(c, f) Then
            o = New flow_block_adapter(f)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function as_flow(ByVal c As TcpClient, ByRef o As flow) As Boolean
        If c Is Nothing Then
            Return False
        Else
            o = New tcp_client_flow(c, Me)
            Return True
        End If
    End Function

    Public Function as_herald(ByVal c As TcpClient) As herald
        Dim h As herald = Nothing
        assert(as_herald(c, h))
        Return h
    End Function

    Public Function as_block(ByVal c As TcpClient) As block
        Dim b As block = Nothing
        assert(as_block(c, b))
        Return b
    End Function

    Public Function as_flow(ByVal c As TcpClient) As flow
        Dim f As flow = Nothing
        assert(as_flow(c, f))
        Return f
    End Function

    '--------------------------------------------------------------------------------

    Public Shared Function as_block(ByVal c As herald, ByRef o As block) As Boolean
        Dim bh As block_herald_adapter = Nothing
        Return cast(Of block_herald_adapter)(c, bh) AndAlso
               as_block(bh, o)
    End Function

    Public Shared Function as_block(ByVal c As block_herald_adapter, ByRef o As block) As Boolean
        If c Is Nothing Then
            Return False
        Else
            o = c.block_dev
            Return True
        End If
    End Function

    Public Shared Function as_flow(ByVal c As block, ByRef o As flow) As Boolean
        Dim fb As flow_block_adapter = Nothing
        If cast(Of flow_block_adapter)(c, fb) Then
            Return as_flow(fb, o)
        Else
            Return False
        End If
    End Function

    Public Shared Function as_flow(ByVal c As flow_block_adapter, ByRef o As flow) As Boolean
        If c Is Nothing Then
            Return False
        Else
            o = c.flow_dev
            Return True
        End If
    End Function

    Public Shared Function as_tcp_client(ByVal h As herald, ByRef o As TcpClient) As Boolean
        Dim b As block = Nothing
        Return as_block(h, b) AndAlso
               as_tcp_client(b, o)
    End Function

    Public Shared Function as_tcp_client(ByVal b As block, ByRef o As TcpClient) As Boolean
        Dim f As flow = Nothing
        Return as_flow(b, f) AndAlso
               as_tcp_client(f, o)
    End Function

    Public Shared Function as_tcp_client(ByVal f As flow, ByRef o As TcpClient) As Boolean
        Dim tf As tcp_client_flow = Nothing
        If cast(Of tcp_client_flow)(f, tf) Then
            Return as_tcp_client(tf, o)
        Else
            Return False
        End If
    End Function

    Private Shared Function as_tcp_client(ByVal f As tcp_client_flow, ByRef o As TcpClient) As Boolean
        If f Is Nothing Then
            Return False
        Else
            o = f.c
            Return True
        End If
    End Function

    Public Shared Function as_tcp_client(ByVal h As herald) As TcpClient
        Dim o As TcpClient = Nothing
        assert(as_tcp_client(h, o))
        Return o
    End Function

    Public Shared Function as_tcp_client(ByVal b As block) As TcpClient
        Dim o As TcpClient = Nothing
        assert(as_tcp_client(b, o))
        Return o
    End Function

    Public Shared Function as_tcp_client(ByVal f As flow) As TcpClient
        Dim o As TcpClient = Nothing
        assert(as_tcp_client(f, o))
        Return o
    End Function
End Class
